package com.fs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.entity.*;
import com.fs.util.StudentsContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.service.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    IStudentsService studentsService;

    @PostMapping("/login")
    public ResponseData login(int sid, String password) {
        studentsService.login(sid, password);
        return ResponseData.ok();
    }

    @GetMapping("/logout")
    //用作退出登录
    public ResponseData logout() {
        //让session失效
        StudentsContext.getSession().invalidate();
        return ResponseData.ok();
    }

    @PostMapping("/register")
    public ResponseData register( Students students) {
        if (studentsService.register(students)) {
            return ResponseData.ok(200, "注册成功");
        } else {
            return ResponseData.error(500, "注册失败");
        }
    }

    //修改个人信息
    @PostMapping("/modify")
    public ResponseData modify(Students students){
        if(studentsService.modify(students)){
            return ResponseData.ok(200,"修改成功");
        }
        else {
            return ResponseData.error(500,"修改失败");
        }
    }
    
    //查询已获得的学分值
    @GetMapping("studentid")
    public ResponseData queryBysid(Integer sid){
       int crd=studentsService.queryBySid(sid);
        return ResponseData.ok(200);
    }

    //查询学生信息
    @GetMapping("/querystuByCondition")
    public ResponseData querystuByCondition(StuCondition stuCondition){
        Page page=studentsService.queryByPageAndCondition(stuCondition);
        System.out.println("学生的班级名称："+stuCondition.getClassname());
        return ResponseData.ok(page);
    }

    //批量修改学生学分
    @PutMapping("/updateStuCredit")
    public ResponseData updateStuCredit(Integer[] sids,Integer credit){
        studentsService.updateStuCredit(sids,credit);
        return ResponseData.ok();
    }
    //查询某个学生正在审核的材料
    @GetMapping("/materials/sid")
    public ResponseData getMaterialsUnderReviewBySid(Integer sid){
        List<Materials> materials=studentsService.getMaterialsUnderReviewBySid(sid);
        if (materials == null || materials.isEmpty()) {
            return ResponseData.error(500,"找不到该学生的材料");
        }
        return ResponseData.ok(materials);
    }


    //推送材料 1.首先查询材料表中 按照材料类别id 进行排序 而后还要顾忌到目前登录的学生有没有排行榜中已经提交过的材料 不给他推送，

    @GetMapping("/materialCategories/sid")
    public ResponseData selectMaterialCategoriesWithoutStudentSubmissions(Integer sid){
        List<Materialcategory> materialcategories=studentsService.selectMaterialCategoriesWithoutStudentSubmissions(sid);
        if(materialcategories==null||materialcategories.isEmpty()){
            return ResponseData.error(500,"目前没有推荐提交的材料");
        }
        return ResponseData.ok(materialcategories);
    }


    //动态查询我的材料
    @GetMapping("/materials/search")
    public ResponseData SelectMaterialsByConditions(StudentCondition condition,Integer sid){
        Page<StuScShow> stuScPage =studentsService.selectMaterialsByConditions(condition,sid);
        return ResponseData.ok(stuScPage);
    }
}
